﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Kugar.Web.MVC
{
    public static class Class1
    {
        public static string GetValue(this FormCollection form, string name, string defaultValue)
        {
            var obj = form.GetValue(name);
            if (obj == null) return defaultValue;
            return obj.AttemptedValue;
        }

        public static string CheckOptionSelected(this HtmlHelper html, object selectValue, object currentValue)
        {

            return CheckValueAndOutPut(html, selectValue, currentValue, "selected = \"selected\"");

            //if (selectValue.Equals(currentValue))
            //{
            //    return "selected = \"selected\"";
            //}
            //else
            //{
            //    return "";
            //}
        }

        public static string CheckRadioSelected(this HtmlHelper html, object selectValue, object currentValue)
        {

            return CheckValueAndOutPut(html, selectValue, currentValue, " checked=\"checked\"");

            //if (selectValue.Equals(currentValue))
            //{
            //    return "selected = \"selected\"";
            //}
            //else
            //{
            //    return "";
            //}
        }

        public static string CheckValueAndOutPut(this HtmlHelper html, object selectValue, object currentValue, string outPutStr)
        {
            if (selectValue.Equals(currentValue))
            {
                return outPutStr;
            }
            else
            {
                return "";
            }
        }

        public static MvcHtmlString RadioButtonForExt<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, bool isAutoChecked)
        {
            return RadioButtonForExt(htmlHelper, ExpressionHelper.GetExpressionText(expression), value, isAutoChecked,
                                     null);
        }

        public static MvcHtmlString RadioButtonForExt<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, bool isAutoChecked, IDictionary<string, object> htmlAttributes)
        {
            string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));

            var ret = expression.Compile()(htmlHelper.ViewData.Model);

            if (isAutoChecked && value.Equals(ret))
            {
                if (htmlAttributes == null)
                {
                    htmlAttributes = new Dictionary<string, object>();
                }

                if (htmlAttributes.ContainsKey("checked"))
                {
                    htmlAttributes.Remove("checked");
                }

                htmlAttributes.Add("checked", "checked");
            }

            return htmlHelper.RadioButton(fullHtmlFieldName.Replace('.', '_'), value, htmlAttributes);



            //return RadioButtonForExt(htmlHelper, ExpressionHelper.GetExpressionText(expression), value, isAutoChecked,
            //                         htmlAttributes);

        }

        public static MvcHtmlString RadioButtonForExt<TModel>(this HtmlHelper<TModel> htmlHelper, string expression, object value, bool isAutoChecked, IDictionary<string, object> htmlAttributes)
        {
            string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expression);

            //if (!htmlHelper.ViewData.ModelState.ContainsKey(expression)) return null;

            var ret = htmlHelper.ViewData.ModelState[fullHtmlFieldName];

            if (isAutoChecked && value.Equals(ret))
            {
                if (htmlAttributes == null)
                {
                    htmlAttributes = new Dictionary<string, object>();
                }

                if (htmlAttributes.ContainsKey("checked"))
                {
                    htmlAttributes.Remove("checked");
                }

                htmlAttributes.Add("checked", "checked");
            }

            return htmlHelper.RadioButton(fullHtmlFieldName.Replace('.', '_'), value, htmlAttributes);
        }
    }
}

